package com.gang.music.system.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gang.music.common.AjaxResult;
import com.gang.music.core.constant.Cst;
import com.gang.music.core.shiro.ShiroKit;
import com.gang.music.core.shiro.ShiroUser;
import com.gang.music.core.toolbox.kit.DateKit;
import com.gang.music.system.entity.PlayRecord;
import com.gang.music.system.entity.Song;
import com.gang.music.system.entity.UserSong;
import com.gang.music.system.service.PlayRecordService;
import com.gang.music.system.service.SongService;
import com.gang.music.system.service.UserSongService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.smallchill.core.toolbox.Func;
import com.smallchill.core.toolbox.kit.FileKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gang
 * @since 2017-10-26
 */
@RestController
@RequestMapping("/song")
public class SongController extends BaseController {
	
	@Autowired
	private SongService songService;
	@Autowired
	private UserSongService userSongService;
	@Autowired
	private PlayRecordService playRecordService;

	/**
	 * 查询所有歌曲
	 * @throws InterruptedException 
	 */
	@RequestMapping(value="/list", method=RequestMethod.POST)
	public Map<String, List<Song>> getSongs() throws InterruptedException {
		Map<String, List<Song>> songMap = Maps.newHashMap();
		songMap.put("list", songService.selectList(null));
		Thread.sleep(3000);
		return songMap;
	}

	/**
	 * 判断是否是认证用户
	 * @return
	 */
	@RequestMapping("/isAuthc")
	public AjaxResult isAuthc() {
		
		return ShiroKit.isUser()?success():error();
	}

	/**
	 * 我的歌曲
	 */
	@RequestMapping("/mySong")
	public Map<String, List<Song>> getMySongs() {
		
		Map<String, List<Song>> songMap = Maps.newHashMap();
		List<Song> songList = Lists.newArrayList();
		ShiroUser shiroUser = ShiroKit.getUser();
		if(shiroUser == null) {
			return null;
		}
		Long userId = shiroUser.getId();

		songList = songService.getMySong(userId);
		songMap.put("list", songList);
		return songMap;
	}
	
	/**
	 *  收藏歌曲
	 */
	@RequestMapping("/saveSong")
	public AjaxResult saveSong(@RequestParam("id")Long songId, Integer hobbyTag) {
		
		try {
			ShiroUser shiroUser = ShiroKit.getUser();
			Long userId = shiroUser.getId();
			UserSong userSong = new UserSong(userId, songId);
			if(hobbyTag == 1) {
				userSongService.save(userSong);
			}else if(hobbyTag == 0) {
				userSongService.delete(userSong);
			}
			return success();
		} catch (Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	//增加播放量
	@RequestMapping("/increase")
	public void increase(@RequestParam("id")Long songId) {
		
		songService.increase(songId);
		
		//只获取年月日,不需要时间,不然没有和数据相等的记录
		Date playDate = DateKit.getDate();
		EntityWrapper<PlayRecord> ew = new EntityWrapper<>();
		ew.eq("play_date", playDate);
		PlayRecord pr = playRecordService.selectOne(ew);
		
		if(Func.isEmpty(pr)) {
			//当天没有播放记录,添加新播放记录
			pr = new PlayRecord(playDate);
			playRecordService.insert(pr);
		}else {
			//当天有播放记录,增加播放次数
			pr.increase();
			playRecordService.updateById(pr);
		}
	}
	
	//把歌词文件转为String返回给前台
	@RequestMapping("/lyric")
	public Map<String, String> getLyric(String lyricName) {
		
		Map<String, String> map = Maps.newHashMap();
		String lyric = "";
		try {
			lyric = 
					FileKit.readString(Cst.me().getUploadRealPath() +"/lyric/" + lyricName, "GBK");
		} catch (IOException e) {
			e.printStackTrace();
		}
		map.put("lyric", lyric);
		return map;
	}
}
